<?php
namespace Org\Oa;
/**
 * 商品操作类
 * @author Administrator
 *
 */
class goods{
	/**
	 * 根据商品查找购买该商品的人还浏览这些
	 */
	public static function buy_goods($goods_id,$listrow){
		$order_goods = M('order_goods as og')->join("left join __ORDER__ as o on o.id = og.order_id")->where("og.goods_id='{$goods_id}' and o.user_id!=''")->group("o.user_id")->field("o.user_id")->select();
	    if(!empty($order_goods)){
	    	$goods_id_array = array();
	    	foreach ($order_goods as $i=>$va){
	    		$goods_id[$i] = M('order_goods as og')->join("left join __ORDER__ as o on o.id = og.order_id")->where("o.user_id='{$va['user_id']}' and og.goods_id!='{$goods_id}'")->group("og.goods_id")->field("og.goods_id")->select();
	    	    foreach ($goods_id[$i] as $k=>$vv){
	    	    	array_push($goods_id_array, $vv['goods_id']);
	    	    }
	    	}
	    	return self::goods_detail($goods_id_array, $listrow);
	    }else{
	    	return self::class_goods($goods_id,$listrow);
	    }
	}
	/**
	 * 获取随机的商品，按照商品分类来获取
	 * $goods_id 查找所关联的商品 
	 */
	public static function class_goods($goods_id=null,$listrow){
		$true_goods_id = array();
		if(!empty($goods_id)){
			if(!is_array($goods_id)){
				$goods_id = array($goods_id);
			}
			$category_ids = array();
			$categoryDb = M('category_extend');
			foreach ($goods_id as $i=>$va){
				array_push($goods_id,$va['goods_id']);
				$categoryid[$i] = $categoryDb->where("goods_id='{$va}'")->field("category_id")->select();
				foreach ($categoryid[$i] as $k=>$vv){
					array_push($category_ids,$vv['category_id']);
				}
			}
			$data['category_id'] = array('in',$category_ids);
			$data['goods_id'] = array('not in',$goods_id);
			$goodsid = $categoryDb->where($data)->group("goods_id")->field("goods_id")->select();
			if(!empty($goods_id)){
				foreach ($goodsid as $k=>$vk){
				   $true_goods_id[] = $vk['goods_id'];
				}
			}
		}
		return self::goods_detail($true_goods_id, $listrow);
	}
	/**
	 * 根据商品的id  查找相关商品的详情
	 */
	public  static function goods_detail($goods_id,$listrow){
		if(!empty($goods_id)){
			if(!is_array($goods_id)){
				$goods_id = array($goods_id);
			}
			$data['id'] = array('in',$goods_id);
		}
		$data['is_del'] = 0;
		$data['status'] = 0;
		$order = "rand() limit {$listrow}";
		$goods = M('goods')->where($data)->field("id,name,img")->order($order)->select();
		$productDb = M('products');
		foreach ($goods as $k=>$vk){
			$goods[$k]['comment_count'] = M('discussion')->where("goods_id='{$vk['id']}'")->count();
			$goods[$k]['product'] = $productDb->where("goods_id='{$vk['id']}' and store_nums>'0'")->field("id,sell_price,market_price,SUBSTRING_INDEX(photo_list,',',1) as product_img,spec_array")->order("sell_price asc")->find();
			if(!empty($goods[$k]['product']['product_img'])){
				$goods[$k]['img'] = $goods[$k]['product']['product_img'];
			}
		}
		return $goods;
	}
	/**
	 * 查找该分类下的所有的商品
	 */
	public static function all_class_goods($cid,$brandid=null,$spec_id=null,$attr_id=null,$start_price,$end_price){
		$goods_id = array();
		$order = "g.id desc";
		if(!empty($brandid)){
			$data['g.brandid'] = $brandid;
		}
		$idarray = array();
		if(!empty($spec_id)){
			$spec_id = explode("@",$spec_id);
			//$spec_goods_id = array();
			foreach ($spec_id as $v=>$vs){
				$spec_id_id[$v] = explode("_",$vs);
				$is[$v] = M('goods_spec')->where("spec_id='{$spec_id_id[$v][0]}' and spec_value='{$spec_id_id[$v][1]}'")->field("goods_id")->select();
				foreach ($is[$v] as $vk=>$vks){
					array_push($idarray, $vks['goods_id']);
				}
			}
			if(empty($idarray)){
				return $goods_id;exit();
			}
			unset($spec_id_id);
			unset($is);
		}
		if(!empty($attr_id)){
			$attr_id = explode("@", $attr_id);
			if(!empty($idarray)){
				$map['goods_id'] = array('in',$idarray);
			}
			$attr_id_array = array();
			foreach ($attr_id as $s=>$vk){
				$attr_id_id[$s] = explode("_",$vk);
				$map['attr_id'] = $attr_id_id[$s][0];
				$map['attr_value'] = $attr_id_id[$s][1];
				$is[$s] = M('goods_attribute')->where($map)->field("goods_id")->select();
				foreach ($is[$s] as $g=>$vg){
					array_push($attr_id_array, $vg['goods_id']);
				}
			}
			if(!empty($attr_id_array)){
				$idarray = $attr_id_array;
			}else{
				return $goods_id;exit();
			}
		}
		//商品列表
		if(!empty($idarray)){
			$data['g.id'] = array('in',$idarray);
		}
		$data['ce.category_id'] = $cid;
		$data['g.is_del'] = 0;
		$data['g.status'] = 0;
		$data['g.name'] = array('neq','');
		$list = M('category_extend as ce')->join("left join __GOODS__ as g on g.id = ce.goods_id")->where($data)->group("g.id")->field("g.id")->order($order)->select();
		if(!empty($list)){
			foreach ($list as $i=>$va){
				$goods_id[] = $va['id'];
			}
			$price = array();
			$price['sell_price'] = array('between',array($start_price,$end_price));
			$price['goods_id'] = array('in',$goods_id);
			$p = M('products')->where($price)->field("goods_id")->group("goods_id")->select();
			if(!empty($p)){
				$new_goods_id = i_array_column($p, 'goods_id');
				return $new_goods_id;
			}else{
				return array();
			}
		}
	}
	/**
	 * 查找关键词的商品
	 */
	public static function all_search_goods($word,$brandid=null,$spec_id=null,$attr_id=null,$start_price,$end_price){
		$goods_id = array();
		$order = "g.id desc";
		if(!empty($brandid)){
			$data['g.brandid'] = $brandid;
		}
		$idarray = array();
		if(!empty($spec_id)){
			$spec_id = explode("@",$spec_id);
			//$spec_goods_id = array();
			foreach ($spec_id as $v=>$vs){
				$spec_id_id[$v] = explode("_",$vs);
				$is[$v] = M('goods_spec')->where("spec_id='{$spec_id_id[$v][0]}' and spec_value='{$spec_id_id[$v][1]}'")->field("goods_id")->select();
				foreach ($is[$v] as $vk=>$vks){
					array_push($idarray, $vks['goods_id']);
				}
			}
			if(empty($idarray)){
				return $goods_id;exit();
			}
			unset($spec_id_id);
			unset($is);
		}
		if(!empty($attr_id)){
			$attr_id = explode("@", $attr_id);
			if(!empty($idarray)){
				$map['goods_id'] = array('in',$idarray);
			}
			$attr_id_array = array();
			foreach ($attr_id as $s=>$vk){
				$attr_id_id[$s] = explode("_",$vk);
				$map['attr_id'] = $attr_id_id[$s][0];
				$map['attr_value'] = $attr_id_id[$s][1];
				$is[$s] = M('goods_attribute')->where($map)->field("goods_id")->select();
				foreach ($is[$s] as $g=>$vg){
					array_push($attr_id_array, $vg['goods_id']);
				}
			}
			if(!empty($attr_id_array)){
				$idarray = $attr_id_array;
			}else{
				return $goods_id;exit();
			}
		}
		//商品列表
		if(!empty($idarray)){
			$data['g.id'] = array('in',$idarray);
		}
		$where['g.name']  = array('like', "%{$word}%");
		$where['g.two_name']  = array('like',"%{$word}%");
		$where['g.search_words'] = array('like',"%{$word}%");
		$where['_logic'] = 'or';
		$data['_complex'] = $where;
		$data['g.is_del'] = 0;
		$data['g.status'] = 0;
		$list = M('goods as g')->where($data)->field("g.id")->order($order)->select();
		if(!empty($list)){
			$goods_id = i_array_column($list, 'id');
			$price = array();
			$price['sell_price'] = array('between',array($start_price,$end_price));
			$price['goods_id'] = array('in',$goods_id);
			$p = M('products')->where($price)->field("goods_id")->group("goods_id")->select();
			if(!empty($p)){
				$new_goods_id = i_array_column($p, 'goods_id');
				return $new_goods_id;
			}else{
				return array();
			}
		}
	}
	/**
	 * 获取搜索的品牌名称
	 */
	public static function search_brand_name($bid){
		if(!empty($bid)){
			$bid = explode("@",$bid);
			foreach ($bid as $i=>$va){
				$name[$i]['name'] = "品牌：".M('brand')->where("id='{$va}'")->getField('name');
				$name[$i]['id'] = $va;
			}
			return $name;
		}
		
	}
	/**
	 * 获取搜索的规格名称
	 */
	public static function search_spec_name($spec_id){
		if(!empty($spec_id)){
			$spec_id = explode("@",$spec_id);
			foreach ($spec_id as $i=>$va){
				$spec = explode("_",$va);
				$name[$i] = M('spec')->where("id='{$va[0]}'")->getField('name');
				$spec_name[$i]['name'] = $name[$i].":".$spec[1];
				$spec_name[$i]['id'] = $va;
			}
			return $spec_name;
		}
		
	}
	/**
	 * 获取搜索的属性名称
	 */
	public static function search_attr_name($attr_id){
		if(!empty($attr_id)){
			$attr_id = explode("@",$attr_id);
			foreach ($attr_id as $i=>$va){
				$attr = explode("_",$va);
				$name[$i] = M('attribute')->where("id='{$va[0]}'")->getField('name');
				$attr_name[$i]['name'] = $name[$i].":".$attr[1];
				$attr_name[$i]['id'] = $va;
			}
			return $attr_name;
		}
		
	}
	/**
	 * 搜素url拼接
	 */
	public static function url($type,$value,$methd=0){
		$url = urldecode(__SELF__);
		$url = str_replace('.html','',$url);
		if($methd == 1){
			$val = urldecode($_REQUEST[$type]);
			if(empty($val)){
				$newurl = $url;
			}else{
				$array = explode("@",$val);
				foreach ($array as $i=>$va){
					if($va == $value){
						unset($array[$i]);
					}
				}
				
				if(empty($array)){
					$list = urldecode($_GET);
					unset($list[$type]);
					
					$newurl = U(CONTROLLER_NAME."/".ACTION_NAME,$list,'');
					//$newurl = str_replace($type."/".$val, "", $url);
				}else{
					$array = array_values($array);
					$str = join("@",$array);
					$newurl = str_replace($type."/".$val, $type."/".$str, $url);
				}
				
				
			}
		}elseif ($methd == 2){
			$val = urldecode($_REQUEST[$type]);
			if(empty($val)){
				$newurl = $url."/".$type."/".$value;
			}else{
				$list = $_GET;
				unset($list[$type]);
				foreach ($list as $i=>$va){
					$list[$i] = urldecode($va);
				}
				$list[$type] = $value;
				$newurl = U(CONTROLLER_NAME."/".ACTION_NAME,$list,'');
			}
		}elseif ($methd == 3){
			//价格组合
			$list = urldecode($_GET);
			if(!empty($list)){
				unset($list[$type]);
				$newurl = U(CONTROLLER_NAME."/".ACTION_NAME,$list,'');
			}else{
				$newurl = $url;
			}
		}else{
			$vv = urldecode($_REQUEST[$type]);
			if(empty($vv)){
				$newurl = $url."/".$type."/".$value;
			}else{
				if(stripos($vv,$value)!==false){
					$newurl = $url;
				}else{
					$oldstr = "/".$type."/".urldecode($_REQUEST[$type]);
					$str = "/".$type."/".urldecode($_REQUEST[$type])."@".$value;
					$newurl = str_replace($oldstr, $str, $url);
				}
					
			}
		}
		
		return self::clearUrl($newurl);
	}
	public static function clearUrl($url)
	{
		return str_replace(array('\'','"','&#',"\\"),'',$url);
	}
}